﻿/// <reference path = "../CellTypes/Base.ts" />
namespace VantCommands {
    interface NotifyParam {
        message?: string;
        type?: string;
        duration?: number;
        position?: "top" | "bottom";
        color: string;
        background: string;
        context: string;
        Command?: Forguncy.Plugin.ICustomCommandObject;
    }

    export class Notify extends Forguncy.Plugin.CommandBase {

        static unclickableClassName = "van-toast--unclickable";

        public async execute(): Promise<void> {
            const param = <NotifyParam>this.CommandParam;
            const clickCommands = param.Command;
            const message = this.evaluateFormula(param.message);
            const context = this.evaluateFormula(param.context);
            let notice = null;

            const toastData = {
                message: message?.toString(),
                type: param.type,
                duration: param.duration ?? 0,
                position: param.position,
                color: Forguncy.ConvertToCssColor(param.color),
                background: param.type === "custom" ? Forguncy.ConvertToCssColor(param.background) : null,
                onClick: () => {
                    if (notice && clickCommands?.Commands && clickCommands.Commands.length > 0) {
                        const param = {};
                        param[clickCommands.ParamProperties["message"]] = message;
                        param[clickCommands.ParamProperties["context"]] = context;
                        this.executeCustomCommandObject(clickCommands, param, "Notice");
                        notice.close();
                    }
                }
            };
            notice = window.vant.Notify(toastData);
        }
    }
}

Forguncy.Plugin.CommandFactory.registerCommand("Vant.Commands.Notify, Vant", VantCommands.Notify);